package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.usian.apis.admin.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.impl.WmUserServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {

    @Autowired
    private WmUserServiceImpl userService;

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUser == null) return userService.errorParam;
        userService.save(wmUser);
        return ResponseResult.okResult(wmUser);
    }

    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {
        return userService.getOne(Wrappers.<WmUser>lambdaQuery().eq(StringUtils.isNotEmpty(name), WmUser::getName, name));
    }

    @PostMapping("updateAuthorId")
    public ResponseResult<WmUser> updateAuthorId(@RequestBody WmUser wmUser) {
        if (wmUser == null) return userService.errorParam;
        userService.updateById(wmUser);
        return userService.succes;
    }

    @GetMapping("/findOne/{id}")
    @Override
    public WmUser findWmUserById(@PathVariable("id") Integer id) {
        return userService.getById(id);
    }
}